package com.dd.machine.main.user;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.cn.dd.machine.R;
import com.dd.machine.base.activity.BaseActivity;
import com.dd.machine.base.app.MyApplication;
import com.dd.machine.greendao.gen.AppGoodsKindDao;
import com.dd.machine.main.commons.VoiceUtils;
import com.dd.machine.main.desktop.DeskTopActivity;
import com.dd.machine.main.horizotalpage.HorizontalPageLayoutManager;
import com.dd.machine.main.horizotalpage.PagingItemDecoration;
import com.dd.machine.main.horizotalpage.PagingScrollHelper;
import com.dd.machine.main.user.adapter.GoodsKindAdapter;
import com.dd.machine.main.user.model.AppGoodsKind;
import com.iflytek.cloud.SpeechSynthesizer;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

import static com.dd.machine.base.net.URLManager.USERLOGOUT;

/**
 * Created by Administrator on 2017/7/3.
 */

public class GoodsKindActivity extends BaseActivity implements PagingScrollHelper.onPageChangeListener {

    RecyclerView recycleView;
    RecyclerView.LayoutManager layoutManager;
    private GoodsKindAdapter adapter;
    PagingScrollHelper scrollHelper = new PagingScrollHelper();

    private RecyclerView.ItemDecoration lastItemDecoration = null;
    private HorizontalPageLayoutManager horizontalPageLayoutManager = null;
    private LinearLayoutManager hLinearLayoutManager = null;
    private LinearLayoutManager vLinearLayoutManager = null;
    private DividerItemDecoration hDividerItemDecoration = null;
    private DividerItemDecoration vDividerItemDecoration = null;
    private PagingItemDecoration pagingItemDecoration = null;

    private static com.baidu.tts.client.SpeechSynthesizer mSpeechSynthesizer;


    private Button btn_exit;

    AppGoodsKindDao appGoodsKindDao;
    List<AppGoodsKind> appGoodsKindList;

    Bundle bundle;
    String userId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_kind);
        bundle = getIntent().getExtras();
        userId = bundle.getString("userId");
        initData();
        init();
        initViews();
        VoiceUtils voiceUtils = MyApplication.getInstances().getVoiceUtils();
        mSpeechSynthesizer = voiceUtils.getSyntheszer();

    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e("-->onResume","onResume");
        mSpeechSynthesizer.speak("欢迎使用当当智能回收机，请选择分类");
    }

    private void initViews() {
        adapter = new GoodsKindAdapter(this,appGoodsKindList);
        recycleView = (RecyclerView) findViewById(R.id.recycleView);
        recycleView.setAdapter(adapter);
        scrollHelper.setUpRecycleView(recycleView);
        scrollHelper.setOnPageChangeListener(this);

        RecyclerView.LayoutManager layoutManager = null;
        RecyclerView.ItemDecoration itemDecoration = null;
        layoutManager = horizontalPageLayoutManager;
        itemDecoration = pagingItemDecoration;

        if (layoutManager != null) {
            recycleView.setLayoutManager(layoutManager);
            recycleView.setHasFixedSize(true);
            recycleView.removeItemDecoration(lastItemDecoration);
            recycleView.addItemDecoration(itemDecoration);
            scrollHelper.updateLayoutManger();
            lastItemDecoration = itemDecoration;
        }

        btn_exit = (Button) findViewById(R.id.btn_exit);
        btn_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                finish();
                logout();

            }
        });
        adapter.setOnItemClickListener(new GoodsKindAdapter.OnItemClickListener(){
            @Override
            public void onItemClick(View view , int position){
                Intent intent = new Intent(GoodsKindActivity.this, KindDetailsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("name",appGoodsKindList.get(position).getTypeName().split(",")[0]);
                bundle.putString("measurement",appGoodsKindList.get(position).getTypeName().split(",")[1]);
                bundle.putString("id",appGoodsKindList.get(position).getGoodsId()+"");
                bundle.putString("userId",userId);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });


    }

    private void logout() {
        OkHttpUtils.post()
                .url(USERLOGOUT)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("==logout-error",e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("==logout-",response);
                        Intent intent = new Intent(GoodsKindActivity.this, DeskTopActivity.class);
                        startActivity(intent);
                        finish();
                    }
                });
    }

    private void init() {
        hLinearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        hDividerItemDecoration = new DividerItemDecoration(this, LinearLayoutManager.HORIZONTAL);

        horizontalPageLayoutManager = new HorizontalPageLayoutManager(2,4);
        pagingItemDecoration = new PagingItemDecoration(this, horizontalPageLayoutManager);


    }

    private void initData() {
        appGoodsKindDao = MyApplication.getInstances().getDaoSession().getAppGoodsKindDao();
        appGoodsKindList = new ArrayList<>();
        appGoodsKindList = appGoodsKindDao.queryBuilder().where(AppGoodsKindDao.Properties.ParentId.eq("no")).list();
        Log.e("===大分类",appGoodsKindList.size()+"");

    }

    @Override
    public void onPageChange(int index) {

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        logout();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        mSpeechSynthesizer.release();
    }
}
